﻿//依赖于jquery.data.js
(function ($) {
    $.fn.echarts = function (options, param) {
        var $this = $(this);
        if ($this.length > 0) {
            $.each($this, function (i, e) {
                var chartType = $(e).attr("charttype");
                var data;
                var _data = $(e).attr("data");
                var _url = $(e).attr("url");
                if (!_data || _data == "") {
                    if (!(!_url || _url == "")) {
                        var  tempdata = fnAajx(_url);
                        if(tempdata&&tempdata!=null)
                        {
                            data = tempdata.data;
                        }
                    }
                }
                else {
                    data = eval("(" + _data + ")");
                }
                switch (chartType) {
                    case "pie":
                        DrawPie(data, $(e).attr("id"));
                        break;
                    case "line":
                        DrawLine(data, $(e).attr("id"));
                        break;
                    case "bar":
                        DrawBar(data, $(e).attr("id"));
                        break;
                    case "k":
                        DrawK(data, $(e).attr("id"));
                        break;
                    case "funnel":
                        DrawFunnel(data, $(e).attr("id"));
                        break;
                    default:
                        break;
                }
            })

        }
        //if (typeof options == 'string') {
        //    return $(this).table.methods[options](this, param);
        //}
    }
    function fnAajx(url, callBack) {
        var _data = null;
        $.ajax({
            type: "GET",
            url: url,
            async: false,
            success: function (data) {
                _data = eval("(" + data + ")");
                if (callBack) {
                    callBack(data);
                }
            },
            error: function (e) {
                return {};
            }
        }
        );
        return _data;
    }
})(jQuery);


$(document).ready(function () {
    $(".echarts").echarts();
});
